package com.usian.article.service.impl;

import com.usian.article.feign.ApBehaviorClient;
import com.usian.article.service.ApArticleBehaviorService;
import com.usian.article.service.ApCollectionService;
import com.usian.model.article.dtos.ArticleInfoDto;
import com.usian.model.article.pojos.ApCollection;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.behavior.pojos.ApFollowBehavior;
import com.usian.model.behavior.pojos.ApLikesBehavior;
import com.usian.model.behavior.pojos.ApUnlikesBehavior;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.user.pojos.ApUser;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: ApArticleBehaviorServiceImpl
 * Author:   WGG
 * Date:    2022-12-2022/12/2-10:18
 * Version: 1.0
 * Description:
 */
@Service
public class ApArticleBehaviorServiceImpl implements ApArticleBehaviorService {

    @Autowired
    private ApBehaviorClient apBehaviorClient;
    @Autowired
    private ApCollectionService apCollectionService;

    @Override
    public ResponseResult loadArticleBehavior(ArticleInfoDto dto) {
        //文章的行为关系主要是在查看文章时展示出来的是否点赞、是否关注、是否不喜欢、是否收藏
        //结果:{"isfollow":true,"islike":true,"isunlike":false,"iscollection":true}
        boolean isfollow = false;
        boolean islike = false;
        boolean isunlike = true;
        boolean iscollection = false;
        ApUser user = AppThreadLocalUtils.getUser();
        ApBehaviorEntry apBehaviorEntry = apBehaviorClient.showBehavior(user.getId(), dto.getEquipmentId());
        Integer id = apBehaviorEntry.getId();
        dto.setId(id);
        ApFollowBehavior apFollowBehavior = apBehaviorClient.showFollowBehavior(dto);

        if (apFollowBehavior!=null){
            isfollow=true;
        }

        ApLikesBehavior apLikesBehavior = apBehaviorClient.showLikesBehavior(dto);

        if (apLikesBehavior!=null){
            islike=true;
        }

        ApUnlikesBehavior apUnlikesBehavior = apBehaviorClient.showUnlikesBehavior(dto);

        if (apUnlikesBehavior!=null){
            isunlike=false;
        }

        ApCollection apCollection = apCollectionService.showCollectionBehavior(dto);

        if (apCollection!=null){
            iscollection=true;
        }

        Map map = new HashMap<>();
        map.put("isfollow",isfollow);
        map.put("islike",islike);
        map.put("isunlike",isunlike);
        map.put("iscollection",iscollection);
        return ResponseResult.okResult(map);
    }
}
